/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.Notes;
import javacommon.util.org.TimeSlice;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.Attach;
import com.tech15.core.model.SysUser;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.dao.MeetingDao;
import com.tech15.pub.dao.MeetingUserDao;
import com.tech15.pub.dao.MsgTipsDao;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.model.Meeting;
import com.tech15.pub.model.MeetingUser;
import com.tech15.pub.model.MsgTips;
import com.tech15.pub.model.PubData;
import com.tech15.pub.vo.query.MeetingQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class MeetingManager extends BaseManager<Meeting,Long>{

	private MeetingDao meetingDao;
	private MeetingUserDao meetingUserDao;
	private MsgTipsDao msgTipsDao;
	private PubDataDao pubDataDao;
	private SysUserDao sysUserDao;
	private AttachManager attachManager;
	
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public void setMsgTipsDao(MsgTipsDao dao) {
		this.msgTipsDao = dao;
	}
	public void setMeetingUserDao(MeetingUserDao dao) {
		this.meetingUserDao = dao;
	}
	public void setMeetingDao(MeetingDao dao) {
		this.meetingDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.meetingDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(MeetingQuery query) {
		Page page = meetingDao.findPage(query);
		List<TimeSlice> tslist = Notes.getTimeSliceList();
		for(Meeting m : (List<Meeting>)page.getResult()){
			String hysj = m.getHysj();
			if(!ObjectUtils.isNullOrEmptyString(hysj)){
				String[] sj = hysj.split(",");
				String strtime = "";
				for(String s : sj){
					TimeSlice ts = tslist.get(Integer.parseInt(s.trim()));
					strtime += ts.getTime();
				}
				if(!ObjectUtils.isNullOrEmptyString(hysj)){
					String s1 = strtime.substring(0,5);
					String s2 = strtime.substring(strtime.length()-5);
					strtime = s1 +'-'+ s2;
				}
				m.setStrtime(strtime);
			}
			
			List<MeetingUser> list = meetingUserDao.searchByMid(m.getId());
			m.setMeetingUsers(list);
		}
		return page;
	}
	
	public Map timeSlice(MeetingQuery query) {
		List<Meeting> list = meetingDao.findMetting(query);
		List<TimeSlice> tslist = Notes.getTimeSliceList();
		Map map = new HashMap();
		for(Meeting m : list){
			String hysj = m.getHysj();
			if(!ObjectUtils.isNullOrEmptyString(hysj)){
				String[] sj = hysj.split(",");
				String strtime = "";
				for(String s : sj){
					TimeSlice ts = tslist.get(Integer.parseInt(s.trim()));
					ts.setIsUsed("true");
					strtime += ts.getTime();
				}
				
				if(!ObjectUtils.isNullOrEmptyString(hysj)){
					String s1 = strtime.substring(0,5);
					String s2 = strtime.substring(strtime.length()-5);
					strtime = s1 +'-'+ s2;
				}
				m.setStrtime(strtime);
			}
		}
		map.put("timeSlice", tslist);
		map.put("meetingList", list);
		return map;
	}
	
	public Meeting getById(Long id){
		Meeting meeting = meetingDao.getById(id);
		List<MeetingUser> list = meetingUserDao.searchByMid(id);
		meeting.setMeetingUsers(list);
		
		List<TimeSlice> tslist = Notes.getTimeSliceList();
		String hysj = meeting.getHysj();
		if(!ObjectUtils.isNullOrEmptyString(hysj)){
			String[] sj = hysj.split(",");
			String strtime = "";
			for(String s : sj){
				TimeSlice ts = tslist.get(Integer.parseInt(s.trim()));
				strtime += ts.getTime();
			}
			if(!ObjectUtils.isNullOrEmptyString(hysj)){
				String s1 = strtime.substring(0,5);
				String s2 = strtime.substring(strtime.length()-5);
				strtime = s1 +'-'+ s2;
			}
			meeting.setStrtime(strtime);
		}
		if(ObjectUtils.isNotEmpty(meeting.getMrid())){
			PubData p = pubDataDao.getById(meeting.getMrid());
			meeting.setDataname(p.getName());
		}
		if(ObjectUtils.isNotEmpty(meeting.getYdr())){
			SysUser user = sysUserDao.getById(meeting.getYdr());
			meeting.setRealname(user.getRealname());
		}
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.TABLE_MEETING, ""+id);
		meeting.setAttach(attach);
		return meeting;
	}
	
	public void saveMeeting(Meeting meeting, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		meetingDao.save(meeting);
		meetingDao.flush();
		if(!ObjectUtils.isNullOrEmptyString(meeting.getAttendUsers())){
			String[] userids = meeting.getAttendUsers().split(",");
			for(String str : userids){
				MeetingUser meetingUser = new MeetingUser();
				meetingUser.setMid(meeting.getId());
				meetingUser.setUserid(new Long(str.trim()));
				meetingUserDao.save(meetingUser);
				
				MsgTips tips = new MsgTips();
				tips.setBt(meeting.getZt());
				tips.setContent(meeting.getZt());
				tips.setTablename(Notes.meeting);
				tips.setTableid(meeting.getId()+"");
				tips.setZt(0);
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUserid(new Long(str.trim()));
				tips.setUrl(Notes.meetingUrl+"id="+meeting.getId());
				msgTipsDao.save(tips);
			}
		}
		
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttach(realpath, meeting.getYdr(), Notes.TABLE_MEETING, ""+meeting.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	public void updateMeeting(Meeting meeting, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		meetingDao.save(meeting);
		meetingDao.flush();
		String userid= meeting.getAttendUsers().replace(" ", "");
		meetingUserDao.deleteByNotInid(userid,meeting);
		if(!ObjectUtils.isNullOrEmptyString(meeting.getAttendUsers())){
			String[] userids = meeting.getAttendUsers().split(",");
			for(String str : userids){
				List<MeetingUser> meetingUses=meetingUserDao.searchByUserId(meeting,str);
				if(ObjectUtils.isEmpty(meetingUses)){
					MeetingUser meetingUser = new MeetingUser();
					meetingUser.setMid(meeting.getId());
					meetingUser.setUserid(new Long(str.trim()));
					meetingUserDao.save(meetingUser);
					
					MsgTips tips = new MsgTips();
					tips.setBt(meeting.getZt());
					tips.setContent(meeting.getZt());
					tips.setTablename(Notes.meeting);
					tips.setTableid(meeting.getId()+"");
					tips.setZt(0);
					tips.setXxsj(DateTools.getCurrentTime());
					tips.setUserid(new Long(str.trim()));
					tips.setUrl(Notes.meetingUrl+"id="+meeting.getId());
					msgTipsDao.save(tips);
				}
			
			}
		}
		
		if(attachFile != null  && attachFileFileName != null){
			attachManager.delete(Notes.TABLE_MEETING, meeting.getId()+"");
			attachManager.saveAttach(realpath, meeting.getYdr(), Notes.TABLE_MEETING, ""+meeting.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		
	}
	public Page searchMyMeeting(MeetingQuery query) {
		Page page= meetingDao.searchMyMeeting(query);
		List<TimeSlice> tslist = Notes.getTimeSliceList();
		for(Meeting m : (List<Meeting>)page.getResult()){
			String hysj = m.getHysj();
			if(!ObjectUtils.isNullOrEmptyString(hysj)){
				String[] sj = hysj.split(",");
				String strtime = "";
				for(String s : sj){
					TimeSlice ts = tslist.get(Integer.parseInt(s.trim()));
					strtime += ts.getTime();
				}
				if(!ObjectUtils.isNullOrEmptyString(hysj)){
					String s1 = strtime.substring(0,5);
					String s2 = strtime.substring(strtime.length()-5);
					strtime = s1 +'-'+ s2;
				}
				m.setStrtime(strtime);
			}
			
			List<MeetingUser> list = meetingUserDao.searchByMid(m.getId());
			m.setMeetingUsers(list);
		}
		return page;
	}
}
